const http = require('../../utils/http.js');
Page({
  data: {
    fileList: [], // 文件列表
  },

  onLoad() {
    // 模拟文件列表的获取
    const url = '/file/api/list';
    const storedUserInfo = wx.getStorageSync('userInfo');
    // console.log('this is userInfo')
    // console.log(storedUserInfo);
    // console.log('________________________________________')
    const data ={
     // userId : storedUserInfo.userId,
     // createBy : storedUserInfo.userName ,
     // classesId : storedUserInfo.classesId ,
      userId : 3,
      createBy : 32109210 ,
      classesId : 110 
    }
    http.get(url,data).then(res=>{
      this.setData({fileList: res.data.data});
    }).catch(err=>{
      console.log(err);
    })
  },

  // 获取文档可见范围的描述
  getFileRangeDescription(range) {
    switch (range) {
      case 0:
        return "全部可见";
      case 1:
        return "指定班级可见";
      case 2:
        return "指定人可见";
      case 3:
        return "指定人不可见";
      case 4:
        return "私有";
      default:
        return "未知";
    }
  },

  onViewDetail(e) {
    const fileId = e.currentTarget.dataset.id;
    wx.showToast({
      title: `查看文件详情: ${fileId}`,
      icon: "none",
    });
    // 这里可以跳转到文件详情页面
  },

  onDownload(e) {
    const fileId = e.currentTarget.dataset.id;
    const url = '/file/api/download';
    const data = {
      fileId :fileId
    }
    http.get(url,data).then(res=>{
      console.log(res);
      wx.showToast({
        title: `下载文件ID: ${fileId}成功`,
        icon: "success",
      });
    }).catch(err=>{
      console.log(err);
      wx.showToast({
        title: `下载文件ID: ${fileId}成功`,
        icon: "success",
      });
    })
  },

  onDelete(e) {
    const fileId = e.currentTarget.dataset.id;
    wx.showModal({
      title: "确认删除",
      content: `确认删除文件ID: ${fileId}？`,
      success: (res) => {
        if (res.confirm) {
          url = '/file/api/delete';
          data = {
            fileId:fileId
          }
          http.get(url,data).then(res => {
            console.log(res);
            const updatedList = this.data.fileList.filter(
              (item) => item.fileId !== fileId
            );
            this.setData({ fileList: updatedList });
            wx.showToast({
              title: "文件已删除",
              icon: "success",
            });
          }).catch(err=>{
            console.log(err);
            wx.showToast({
              title: '删除文件失败',
            })
          })
          
        }
      },
    });
  },
});
